package com.woniu.tms.manage.controller;

import com.woniu.tms.entity.DB;
import com.woniu.tms.manage.service.DataService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.nio.charset.StandardCharsets;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("manage/data")
public class DataController {

    @Autowired
    DataService dataService;

    /**
     * 跳转展示数据库备份文件页面
     *
     * @return
     */
    @RequestMapping("index")
    public String index() {
        return "WEB-INF/manage/data/index";
    }

    /**
     * 产生数据库临时备份文件
     *
     * @param request
     * @return 备份文件列表
     */
    @RequestMapping("indexDo")
    @ResponseBody
    public Map<String, Object> indexDo(HttpServletRequest request) {
        Map<String, Object> map = new HashMap<>();
        try {
            List<DB> dbList = dataService.indexDo(request);
            map.put("dbList", dbList);
            map.put("result", true);
        } catch (Exception e) {
            map.put("result", false);
        }
        return map;
    }

    /**
     * 导出数据库备份文件
     *
     * @param request
     * @param response
     * @param name
     */
    @RequestMapping("download")
    public void download(HttpServletRequest request, HttpServletResponse response, String name) {
        //文件在项目中的路径
        String filePath = null;
        filePath = request.getSession().getServletContext().getRealPath("datas") + "/" + name + ".sql";
        System.out.println("filePath = " + filePath);
        File outfile = new File(filePath);
        // 获取文件名称
        String filename = outfile.getName();
        InputStream fis = null;
        OutputStream os = null;
        try {
            fis = new BufferedInputStream(new FileInputStream(filePath));
            byte[] buffer = new byte[fis.available()];
            //读取文件流
            fis.read(buffer);
            fis.close();
            //重置结果集
            response.reset();
            //返回头 文件名
            response.addHeader("Content-Disposition", "attachment;filename=" + new String(filename.replaceAll(" ", "").getBytes(StandardCharsets.UTF_8), "iso8859-1"));
            //返回头 文件大小
            response.addHeader("Content-Length", "" + outfile.length());
            //设置数据种类
            response.setContentType("application/octet-stream");
            //获取返回体输出权
            os = new BufferedOutputStream(response.getOutputStream());
            // 输出文件
            os.write(buffer);
            os.flush();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                if (fis != null) {
                    fis.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
            try {
                if (os != null) {
                    os.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    /**
     * 恢复数据库
     *
     * @param request、
     * @param name     文件名字
     * @return
     */
    @RequestMapping("recover")
    @ResponseBody
    public Map<String, Object> recover(HttpServletRequest request, String name) {
        Map<String, Object> map = new HashMap<>();
        try {
            Boolean flag = dataService.recover(request, name);
            map.put("result", flag);
        } catch (Exception e) {
            map.put("result", false);
        }
        return map;
    }

}
